package com.shop.milk_tea_shop.controller;

import com.shop.milk_tea_shop.common.CodeEnum;
import com.shop.milk_tea_shop.common.GlobalInfo;
import com.shop.milk_tea_shop.common.Result;
import com.shop.milk_tea_shop.entity.EmployeeInfo;
import com.shop.milk_tea_shop.request.EmployeeQueryRequest;
import com.shop.milk_tea_shop.service.EmployeeInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 职员信息表：	存储最高管理员admin、店长店员等职员信息。 前端控制器
 * </p>
 *
 * @author yjw
 * @since 2024-06-16
 */
@RestController
@RequestMapping("/employeeInfo")
@Slf4j
public class EmployeeInfoController {

    @Autowired
    EmployeeInfoService employeeInfoService;

    @PostMapping("/paged")
    public Result<List<EmployeeInfo>> paged(EmployeeQueryRequest request) {
        return employeeInfoService.paged(request);
    }

    @GetMapping("/{id}")
    public Result<EmployeeInfo> getById(@PathVariable Long id) {
        EmployeeInfo employeeInfo = employeeInfoService.getById(id);
        return new Result<>(CodeEnum.SUCCESS.getMsg(), CodeEnum.SUCCESS, employeeInfo, new GlobalInfo());
    }

    @PostMapping("/add")
    public Result<Boolean> add(EmployeeInfo employee) {
        boolean result = employeeInfoService.save(employee);
        return new Result<>(CodeEnum.SUCCESS.getMsg(), CodeEnum.SUCCESS, result, new GlobalInfo());
    }

    @PutMapping
    public Result<Boolean> update(EmployeeInfo employee) {
        Long id = Thread.currentThread().getId();
        log.info("线程id为：{}",id);
        boolean result = employeeInfoService.updateById(employee);
        return new Result<>(CodeEnum.SUCCESS.getMsg(), CodeEnum.SUCCESS, result, new GlobalInfo());
    }

    @DeleteMapping("/{id}")
    public Result<Boolean> delete(@PathVariable Long id) {
        EmployeeInfo employee = new EmployeeInfo();
        employee.setEmployeeId(id);
        employee.setStatus(0);
        return update(employee);
    }
}
